﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Demo4
{
    /// <summary>
    /// 简单的银行交易类
    /// </summary>

    public class BankAccount
    {
        private string accountSerials;   // 流水号
        private string accountId;   // 账户
        private decimal balance;   // 余额

        //构造方法
        public BankAccount(string accountId, decimal balance)
        {
            this.accountId = accountId;
            this.balance = balance;
            // 处理流水号
            this.accountSerials = DateTime.Now.ToString("yyyyMMddhhmmssfff") 
                +  new Random().Next(0, 1000).ToString().PadLeft (3,'0');
            Console.WriteLine("日志：创建交易");
            Console.WriteLine($"流水号：{accountSerials}\t账户：{accountId}\t余额：{balance:c2}");
        }
        
        // 存入
        public void Deposit(decimal amount)
        {
            this.balance += amount;
            Console.WriteLine($"日志：存入{amount}");
            Console.WriteLine($"流水号：{accountSerials}\t账户：{accountId}\t余额：{balance:c2}");
        }

        // 取出
        public bool Withdraw(decimal amount)
        {
            if (balance > amount)
            {
                balance -= amount;
                Console.WriteLine($"日志：取出{amount}成功");
                Console.WriteLine($"流水号：{accountSerials}\t账户：{accountId}\t余额：{balance:c2}");
                return true;
            }
            else
            {
                Console.WriteLine($"日志：取出{amount}失败");
                Console.WriteLine($"流水号：{accountSerials}\t账户：{accountId}\t余额：{balance:c2}");
                return false;
            }
        }
    }
}
